package cn.buk.tms.eotms.report.dto;

import cn.buk.tms.eotms.entity.BillFlightTicket;
import cn.buk.tms.eotms.entity.BillFlightTicketDetail;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import org.springframework.beans.BeanUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * 输出报表时的机票内容：融合了出票，退票，改签的信息，方便在页面展示的时候，
 * 尽量将同一张票号的退票、改签放在上下挨着的
 */
public class ReportTicketDto {

	public static ReportTicketDto createReportTicketDto(BillFlightTicket ticket) {
		ReportTicketDto dto = new ReportTicketDto();

		BeanUtils.copyProperties(ticket, dto);

		dto.amount = ticket.getAccountRecv();

		dto.setDetails(new ArrayList<>());
		for(BillFlightTicketDetail detail: ticket.getDetails()) {
			ReportTicketDetailDto detailDto = new ReportTicketDetailDto();

			BeanUtils.copyProperties(detail, detailDto);
			dto.details.add(detailDto);
		}

		return dto;
	}


	@JsonIgnore
	private String balanceCode;
	@JsonIgnore
	private String ticketNo;

	/**
	 * 改签后的新票号
	 */
//	@JsonInclude(JsonInclude.Include.NON_NULL)
	@JsonIgnore
	private String newTicketNo;

	@JsonIgnore
	private String billNum; // 对应的账单号

	/**
	 * 0 - 出票
	 * 1 - 退票
	 * 2 - 改签
	 */
	private int ticketType;

	/**
	 * 票号数量（联程客票）
	 */
	@JsonIgnore
	private Integer ticketCount;

	/**
	 * 截止票号
	 */
	@JsonIgnore
	private String ticketNoEnd;


	private String passengerName;

	private double amount; // 应收金额

	@JsonIgnore
	private String remark;

	/**
	 * 公布运价
	 */
	@JsonIgnore
	private double price;

	/**
	 * 实际销售运价
	 */
	@JsonIgnore
	private double parvalue;


	private Double tax;

	/**
	 * 机场税
	 */
	@JsonIgnore
	private double airportTax;

	/**
	 * 燃油附加费
	 */
	@JsonIgnore
	private double fuelSurcharge;

	@JsonIgnore
	private double commissionRate;

	@JsonIgnore
	private double commissionRateExt;

	@JsonIgnore
	private double commission;

	@JsonIgnore
	private double customerCommissionRate;

	@JsonIgnore
	private double discount;

	@JsonIgnore
	private double discount2;

	@JsonIgnore
	private double hcharge;

	/**
	 * 服务费
	 */
	@JsonIgnore
	private double serviceCharge;

	/**
	 * 保险
	 */
	@JsonIgnore
	private double insurance;

	@JsonIgnore
	private double lowFare;

	/**
	 * 出票日期
	 */
	@JsonIgnore
//	@JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
	private Date etdzDate;

	/**
	 * 订单日期
	 */
	@JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
	private Date orderDate;

	private List<ReportTicketDetailDto> details;

	/**
	 * 第一个行程的出发日期（用于拍讯）
	 * @return
	 */
	@JsonIgnore
	public String getDdate() {
		if (details == null) {
      return  null;
    }
		if (details.size() == 0) {
      return null;
    }

		return details.get(0).getDdate();
	}

	/**
	 * 如果有新票号，则显示新票号；
	 * 如果没有，则显示老票号
	 * @return
	 */
	public String getShowTicketNo() {
		boolean b = newTicketNo == null || newTicketNo.trim().length() == 0;

		return b ? balanceCode + "-" + ticketNo : newTicketNo;
	}

	/**
	 * 合并了服务费等的销售价
	 * @return
	 */
	public double getShowParValue() {
		double x = 0;

		if (this.ticketType == 0) {
			//出票
			x = this.parvalue + this.insurance + this.serviceCharge - this.discount;
		} else if (this.ticketType == 1) {
			//退票
			x = this.amount;
		} else if (this.ticketType == 2) {
			//改签
			x = this.amount;
		}
		return x;
	}



	public String getTicketNo() {
		return ticketNo;
	}

	public void setTicketNo(String ticketNo) {
			this.ticketNo = ticketNo;
	}

	public String getPassengerName() {
		return passengerName;
	}

	public void setPassengerName(String passengerName) {
		this.passengerName = passengerName;
	}

	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

	public double getPrice() {
		return price;
	}

	public void setPrice(double price) {
		this.price = price;
	}

	public double getParvalue() {
		return parvalue;
	}

	public void setParvalue(double parvalue) {
		this.parvalue = parvalue;
	}

	public Double getTax() {
		// 出票才显示税收，
		// 退票，改签都显示0
		return this.ticketType == 0 ? tax : null;
	}

	public void setTax(Double tax) {
		this.tax = tax;
	}

	public double getCommissionRate() {
		return commissionRate;
	}

	public void setCommissionRate(double commissionRate) {
		this.commissionRate = commissionRate;
	}

	public double getCommissionRateExt() {
		return commissionRateExt;
	}

	public void setCommissionRateExt(double commissionRateExt) {
		this.commissionRateExt = commissionRateExt;
	}

	public double getCommission() {
		return commission;
	}

	public void setCommission(double commission) {
		this.commission = commission;
	}

	public double getCustomerCommissionRate() {
		return customerCommissionRate;
	}

	public void setCustomerCommissionRate(double customerCommissionRate) {
		this.customerCommissionRate = customerCommissionRate;
	}

	public double getDiscount() {
		return discount;
	}

	public void setDiscount(double discount) {
		this.discount = discount;
	}

	public double getDiscount2() {
		return discount2;
	}

	public void setDiscount2(double discount2) {
		this.discount2 = discount2;
	}

	public double getHcharge() {
		return hcharge;
	}

	public void setHcharge(double hcharge) {
		this.hcharge = hcharge;
	}

	public double getServiceCharge() {
		return serviceCharge;
	}

	public void setServiceCharge(double serviceCharge) {
		this.serviceCharge = serviceCharge;
	}

	public double getLowFare() {
		return lowFare;
	}

	public void setLowFare(double lowFare) {
		this.lowFare = lowFare;
	}

	/**
	 * @return the airportTax
	 */
	public double getAirportTax() {
		return airportTax;
	}

	/**
	 * @param airportTax the airportTax to set
	 */
	public void setAirportTax(double airportTax) {
		this.airportTax = airportTax;
	}

	/**
	 * @return the fuelSurcharge
	 */
	public double getFuelSurcharge() {
		return fuelSurcharge;
	}

	/**
	 * @param fuelSurcharge the fuelSurcharge to set
	 */
	public void setFuelSurcharge(double fuelSurcharge) {
		this.fuelSurcharge = fuelSurcharge;
	}

	public String getBalanceCode() {
		return balanceCode;
	}

	public void setBalanceCode(String balanceCode) {
		this.balanceCode = balanceCode;
	}

	public Date getEtdzDate() {
		return etdzDate;
	}

	public void setEtdzDate(Date etdzDate) {
		this.etdzDate = etdzDate;
	}

	public Integer getTicketCount() {
		return ticketCount == null ? 1 : ticketCount;
	}

	public void setTicketCount(Integer ticketCount) {
		this.ticketCount = ticketCount;
	}

	public String getTicketNoEnd() {
		return ticketNoEnd;
	}

	public void setTicketNoEnd(String ticketNoEnd) {
		this.ticketNoEnd = ticketNoEnd;
	}

	public void setDetails(List<ReportTicketDetailDto> details) {
		this.details = details;
	}

	public int getTicketType() {
		return ticketType;
	}

	public void setTicketType(int ticketType) {
		this.ticketType = ticketType;
	}

    public double getAmount() {
        BigDecimal bg = new BigDecimal(this.amount);
        this.amount = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        return amount;
    }

    public void setAmount(double amount) {
        this.amount = amount;
    }

	/**
	 * 该票号包含的行程信息
	 */
	public List<ReportTicketDetailDto> getDetails() {
		if (details == null) {
      details = new ArrayList<>();
    }
		return details;
	}

	public String getBillNum() {
		return billNum;
	}

	public void setBillNum(String billNum) {
		this.billNum = billNum;
	}

	public Date getOrderDate() {
		return orderDate;
//		return orderDate == null ? DateUtil.getTomorrowDate() : orderDate; // 为了调用Comparator时不为null
	}

	public void setOrderDate(Date orderDate) {
		this.orderDate = orderDate;
	}

	public double getInsurance() {
		return insurance;
	}

	public void setInsurance(double insurance) {
		this.insurance = insurance;
	}

	public String getNewTicketNo() {
		return newTicketNo;
	}

	public void setNewTicketNo(String newTicketNo) {
		this.newTicketNo = newTicketNo;
	}
}
